class Solution {
public:
    struct Project
    {
        int profits;
        int cost;

        Project(int pro, int cs) : profits(pro), cost(cs) {}
    };

    struct ProfitCmp
    {
        bool operator() (const Project& p1, const Project& p2)
        {
            return p1.profits < p2.profits;
        }
    };

    struct CostCmp
    {
        bool operator() (const Project& p1, const Project& p2)
        {
            return p1.cost > p2.cost;
        }
    };
    int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) {
        priority_queue<Project, vector<Project>, ProfitCmp> qp;
        priority_queue<Project, vector<Project>, CostCmp> qc;

        int n = profits.size();
        for (int i = 0; i < n; ++i)
            qc.push(Project(profits[i], capital[i]));

        for (int i = 0; i < k; ++i)
        {
            while (!qc.empty() && w >= qc.top().cost)
            {
                qp.push(qc.top());
                qc.pop();
            }
            if(!qp.empty())
            {
                w += qp.top().profits;
                qp.pop();
            }
        }

        return w;
    }
};
